using UnityEngine;
using System.Collections;

public class FourDirectionController : MonoBehaviour 
{
	enum Direction{UP, DOWN, LEFT, RIGHT};
	Direction direction;
	bool isMoving = false;
	public float speed = 5;
	
	public enum Controller{P1, P2, P3, P4};
	public Controller index;
	// Use this for initialization
	void Start () 
	{
		
	}
	
	// Update is called once per frame
	void Update () 
	{
		switch(index)
		{
			case Controller.P1:
				Movement("HorizontalP1","VerticalP1");
				break;
			case Controller.P2:
				Movement("HorizontalP2","VerticalP2");
				break;
		}
		
	}
	/// <summary>
	/// responsable for moving the object.
	/// </summary>
	void Movement(string horizontalAxis, string verticalAxis)
	{
		
		float moveVertical = 0;
		float moveHorizontal = 0;
		
		//receives the values of vertical and horizontal axis.
		moveVertical = Input.GetAxis(verticalAxis)*Time.deltaTime*speed;
		moveHorizontal = Input.GetAxis(horizontalAxis)*Time.deltaTime*speed;
		
		//here it checks if it is moving vertically or horizontally, and assigns proper direction.
		//note that it can't move on diagonals
		if(moveVertical < 0)
		{
			this.transform.rotation = Quaternion.Euler(0,0,0);
			this.direction = Direction.UP;
			this.isMoving = true;
			this.transform.position += new Vector3(0,moveVertical,0f);
		}
		else if(moveVertical > 0)
		{
			this.transform.rotation = Quaternion.Euler(0,0,180);
			this.direction = Direction.DOWN;
			this.isMoving = true;
			this.transform.position += new Vector3(0,moveVertical,0f);
		}
		else if(moveHorizontal > 0)
		{
			this.transform.rotation = Quaternion.Euler(0,0,90);
			this.direction = Direction.RIGHT;
			this.isMoving = true;
			this.transform.position += new Vector3(moveHorizontal,0,0f);
		}
		else if(moveHorizontal <0)
		{
			this.transform.rotation = Quaternion.Euler(0,0,-90);
			this.direction = Direction.LEFT;
			this.isMoving = true;
			this.transform.position += new Vector3(moveHorizontal,0,0f);
		}
		else
		{
			this.isMoving = false;
		}
		
	}
}
